﻿//------------------------------------------------------------------
// System Name:    NRemedy
// Component:      NRemedy
// Create by:      Parker Zhou (parkerz@wicresoft.com)
// Create Date:    2012-04-10
//------------------------------------------------------------------
using System;
using System.Collections;
using BMC.ARSystem;


namespace NRemedy
{
    /// <summary>
    /// implement of IARServer,just using BMC.ARSystem.Server
    /// </summary>
    public class ARServer : IARServer
    {
        protected Server serverInstance;

        public void Login(string server, string user, string password, string authentication)
        {
            if (serverInstance == null)
                serverInstance = new Server();
            serverInstance.Login(server, user, password, authentication);
        }

        public void Logout()
        {
            if (serverInstance == null)
                throw new NullReferenceException("serverInstance cannot perform logout, since it is null, login may not be performed before.");
            serverInstance.Logout();
        }

        public ArrayList GetListForm()
        {
            if (serverInstance == null)
                throw new NullReferenceException("serverInstance cannot perform GetListForm, since it is null, login may not be performed before.");
            return serverInstance.GetListForm();
        }

        public ARForm GetForm(string name)
        {
            if (serverInstance == null)
                throw new NullReferenceException("serverInstance cannot perform GetForm, since it is null, login may not be performed before.");
            return serverInstance.GetForm(name);
        }

        public ArrayList GetListField(string formName)
        {
            if (serverInstance == null)
                throw new NullReferenceException("serverInstance cannot perform GetListField, since it is null, login may not be performed before.");
            return serverInstance.GetListField(formName);
        }

        public Field GetField(string formName, uint id)
        {
            if (serverInstance == null)
                throw new NullReferenceException("serverInstance cannot perform GetField, since it is null, login may not be performed before.");
            return serverInstance.GetField(formName, id);
        }


        public void BeginBulkEntryTransaction()
        {
            if (serverInstance == null)
                throw new NullReferenceException("serverInstance cannot perform BeginBulkEntryTransaction, since it is null, login may not be performed before.");
            serverInstance.BeginBulkEntryTransaction();
        }

        public ArrayList CancelBulkEntryTransaction()
        {
            if (serverInstance == null)
                throw new NullReferenceException("serverInstance cannot perform CancelBulkEntryTransaction, since it is null, login may not be performed before.");
            return serverInstance.CancelBulkEntryTransaction();
        }

        public ArrayList SendBulkEntryTransaction()
        {
            if (serverInstance == null)
                throw new NullReferenceException("serverInstance cannot perform SendBulkEntryTransaction, since it is null, login may not be performed before.");
            return serverInstance.SendBulkEntryTransaction();
        }


        public string CreateEntry(string form, FieldValueList fieldValueList)
        {
            if (serverInstance == null)
                throw new NullReferenceException("serverInstance cannot perform CreateEntry, since it is null, login may not be performed before.");
            return serverInstance.CreateEntry(form, fieldValueList);
        }

        public void DeleteEntry(string form, string entryId, int deleteOption = 0)
        {
            if (serverInstance == null)
                throw new NullReferenceException("serverInstance cannot perform DeleteEntry, since it is null, login may not be performed before.");
            serverInstance.DeleteEntry(form, entryId, 0);
        }


        public void SetEntry(string form, string entryId, FieldValueList fieldValueList)
        {
            if (serverInstance == null)
                throw new NullReferenceException("serverInstance cannot perform SetEntry, since it is null, login may not be performed before.");
            serverInstance.SetEntry(form, entryId, fieldValueList);
        }


        public void SetEntryByQuery(string formName, string qualification, Server.SetEntryByQuery_NoMatchOption noMatchOption, Server.SetEntryByQuery_MultiMatchOption multiMatchOption, FieldValueList fieldValueList, bool useLocale = false)
        {
            if (serverInstance == null)
                throw new NullReferenceException("serverInstance cannot perform SetEntryByQuery, since it is null, login may not be performed before.");
            var ccc = (serverInstance.ParseQualification(formName, qualification));
            serverInstance.SetEntryByQuery(formName, (string)(serverInstance.ParseQualification(formName, qualification)), useLocale, noMatchOption, multiMatchOption, fieldValueList);
        }


        public FieldValueList GetEntry(string form, string entryId, FieldIdList fieldIdList = null)
        {
            if (serverInstance == null)
                throw new NullReferenceException("serverInstance cannot perform GetEntry, since it is null, login may not be performed before.");
            if (fieldIdList == null || fieldIdList.Count == 0)
                return serverInstance.GetEntry(form, entryId);
            return serverInstance.GetEntry(form, entryId, fieldIdList);
        }

        public ArrayList GetListEntry(string formName, string qualification, FieldIdList fieldIdList = null)
        {
            if (serverInstance == null)
                throw new NullReferenceException("serverInstance cannot perform GetListEntry, since it is null, login may not be performed before.");
            var ccc = (serverInstance.ParseQualification(formName, qualification));
            if (fieldIdList == null || fieldIdList.Count == 0)
            {
                return serverInstance.GetListEntry(formName,qualification);
            }
            return serverInstance.GetListEntry(formName, qualification);
        }




        public bool ServerAlive
        {
            get
            {
                if (serverInstance == null)
                    throw new NullReferenceException("serverInstance cannot perform CreateEntry, since it is null, login may not be performed before.");
                try
                {
                    bool isAdmin, isSubAdmin, isCustomize;

                    serverInstance.VerifyUser(out isAdmin, out isSubAdmin, out isCustomize);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }
    }

}
